﻿
// MFCView.cpp: CMFCView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MFC.h"
#endif

#include "MFCDoc.h"
#include "MFCView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCView

IMPLEMENT_DYNCREATE(CMFCView, CView)

BEGIN_MESSAGE_MAP(CMFCView, CView)
    // 标准打印命令
    ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
    ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

// CMFCView 构造/析构

CMFCView::CMFCView() noexcept
{
    m_strText = _T("HelloWOrld");
    m_ptString = 0;
}

CMFCView::~CMFCView()
{
}

BOOL CMFCView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: 在此处通过修改
    //  CREATESTRUCT cs 来修改窗口类或样式

    return CView::PreCreateWindow(cs);
}

// CMFCView 绘图

void CMFCView::OnDraw(CDC* pDC)
{
    CMFCDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc) return;

    // TODO: 在此处为本机数据添加绘制代码

    pDC->TextOutW(m_ptString.x, m_ptString.y, m_strText);
    m_ptOriginal = m_ptString;
}


// CMFCView 打印

BOOL CMFCView::OnPreparePrinting(CPrintInfo* pInfo)
{
    // 默认准备
    return DoPreparePrinting(pInfo);
}

void CMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: 添加额外的打印前进行的初始化过程
}

void CMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: 添加打印后进行的清理过程
}


// CMFCView 诊断

#ifdef _DEBUG
void CMFCView::AssertValid() const
{
    CView::AssertValid();
}

void CMFCView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

CMFCDoc* CMFCView::GetDocument() const // 非调试版本是内联的
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCDoc)));
    return (CMFCDoc*)m_pDocument;
}
#endif //_DEBUG


// CMFCView 消息处理程序


void CMFCView::OnInitialUpdate()
{
    CView::OnInitialUpdate();

    m_DropTarget.Register(this);
}


DROPEFFECT CMFCView::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
    return CView::OnDragEnter(pDataObject, dwKeyState, point);
}


void CMFCView::OnDragLeave()
{
    m_ptString = m_ptOriginal;
    CView::OnDragLeave();
}

DROPEFFECT CMFCView::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
    m_ptString = point;
    if (dwKeyState & MK_CONTROL) return DROPEFFECT_COPY;
    return DROPEFFECT_MOVE;
}


BOOL CMFCView::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
{
    HGLOBAL hGlobal = pDataObject->GetGlobalData(CF_TEXT);
    PCHAR lpString = (PCHAR)GlobalLock(hGlobal);

    m_strText = lpString;

    GlobalUnlock(hGlobal);
    GlobalFree(hGlobal);

    Invalidate();

    return TRUE;
}


void CMFCView::OnLButtonDown(UINT nFlags, CPoint point)
{
    int nLength = m_strText.GetLength();
    COleDataSource* lpOleDataSource = new COleDataSource;
    HGLOBAL hGlobal = ::GlobalAlloc(GMEM_ZEROINIT, nLength + 1);
    PCHAR lpString = (PCHAR)::GlobalLock(hGlobal);
    LPTSTR lpOutString = m_strText.GetBuffer();
    DROPEFFECT Effect;

USES_CONVERSION;
    RtlCopyMemory(lpString, T2A(lpOutString), nLength);
    ::GlobalUnlock(hGlobal);
    lpOleDataSource->CacheGlobalData(CF_TEXT, hGlobal);

    Effect = lpOleDataSource->DoDragDrop();
    if (Effect == DROPEFFECT_MOVE)
    {
        m_strText = _T("");
    }
    Invalidate();

    CView::OnLButtonDown(nFlags, point);
}
